package com.mj.app.health.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.mj.app.health.MyApplication;
import com.mj.app.health.R;
import com.mj.app.health.adapter.ServiceAdapter;
import com.mj.app.health.base.BaseActivity;
import com.mj.app.health.bean.ServiceBean;
import com.mj.app.health.customer.ListViewNoScroll;
import com.mj.app.health.defination.IntentKeys;
import com.mj.app.health.net.HttpGo;
import com.mj.app.health.net.HttpListener;
import com.mj.app.health.net.response.ServiceResponse;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
/**
 * ========================================
 * *
 * title: ServiceActivity
 * description: 服务列表
 * *
 * Created by David Hogan on 2018/3/2.
 * ========================================
 */

public class ServiceActivity extends BaseActivity {

    @BindView(R.id.s_lv)
    ListViewNoScroll sLv;
    @BindView(R.id.com_back)
    ImageView comBack;
    @BindView(R.id.com_title)
    TextView comTitle;

    private List<ServiceBean> serviceBeans = new ArrayList<>();
    private ServiceAdapter adapter;

    private Handler mHandler;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_service;
    }

    @Override
    protected void init() {

        Bundle b = getIntent().getBundleExtra(IntentKeys.BUNDLE_KEY);

        if(b != null){

            final int type = b.getInt(IntentKeys.WEB_FROM);

            sLv.setAdapter(adapter = new ServiceAdapter(this));

            mHandler = new Handler() {
                @Override
                public void handleMessage(final Message msg) {

                    if(msg.what == 1){

                        adapter.setServiceBeans(serviceBeans);
                    }else{

                    }
                }
            };

            new Thread(new Runnable() {
                @Override
                public void run() {

                    final Message msg = new Message();

                    HttpGo.loadService(MyApplication.getUser().getUserId(), 0, 10, type,
                            new HttpListener<ServiceResponse>() {
                                @Override
                                public void onSuccess(ServiceResponse serviceResponse) {
                                    serviceBeans = serviceResponse.getData();

                                    msg.what = 1;
                                    mHandler.sendMessage(msg);
                                }

                                @Override
                                public void onError(String exception) {

                                }
                            });
                }
            }).start();


            sLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Bundle b = new Bundle();
                    b.putString(IntentKeys.WEB_TITLE, serviceBeans.get(position).getPaperTitle());
                    b.putString(IntentKeys.WEB_CONTENT,serviceBeans.get(position).getPaperContent());
                    redirectTo(WebNormalActivity.class, false, b);
                }
            });

            switch (type){
                case 1:
                    comTitle.setText("少年");
                    break;
                case 2:
                    comTitle.setText("青年");
                    break;
                case 3:
                    comTitle.setText("老年");
                    break;
                case 4:
                    comTitle.setText("减肥计划");
                    break;
                case 5:
                    comTitle.setText("增肌计划");
                    break;
            }
        }

        comBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });

    }
}
